# use normal make for this Makefile
#
# Makefile for building user programs to run on top of Nachos
#
# Several things to be aware of:
#
#    Nachos assumes that the location of the program startup routine (the
# 	location the kernel jumps to when the program initially starts up)
#       is at location 0.  This means: start.o must be the first .o passed 
# 	to ld, in order for the routine "Start" to be loaded at location 0
#

# if you are cross-compiling, you need to point to the right executables
# and change the flags to ld and the build procedure for as

GCCDIR = ../../../gnu-decstation-ultrix/decstation-ultrix/2.95.3/

LDFLAGS = -T script -N
ASFLAGS = -mips2
CPPFLAGS = $(INCDIR)

# if you aren't cross-compiling:
# GCCDIR =
# LDFLAGS = -N -T 0
# ASFLAGS =
# CPPFLAGS = -P $(INCDIR)

PATH = $(GCCDIR):/lib:/usr/bin:/bin

CC = $(GCCDIR)gcc -B../../../gnu-decstation-ultrix/
AS = $(GCCDIR)as
LD = $(GCCDIR)ld

CPP = gcc -E
INCDIR =-I../userprog -I../threads
CFLAGS = -G 0 -c $(INCDIR)

all: halt shell matmult sort proj proj2 ls pwd create open close read write

start.o: start.s ../userprog/syscall.h
	$(CPP) $(CPPFLAGS) start.c > strt.s
	$(AS) $(ASFLAGS) -o start.o strt.s
	rm strt.s

halt.o: halt.c
	$(CC) $(CFLAGS) -c halt.c
halt: halt.o start.o
	$(LD) $(LDFLAGS) start.o halt.o -o halt.coff
	../bin/coff2noff halt.coff halt

shell.o: shell.c
	$(CC) $(CFLAGS) -c shell.c
shell: shell.o start.o
	$(LD) $(LDFLAGS) start.o shell.o -o shell.coff
	../bin/coff2noff shell.coff shell

sort.o: sort.c
	$(CC) $(CFLAGS) -c sort.c
sort: sort.o start.o
	$(LD) $(LDFLAGS) start.o sort.o -o sort.coff
	../bin/coff2noff sort.coff sort

matmult.o: matmult.c
	$(CC) $(CFLAGS) -c matmult.c
matmult: matmult.o start.o
	$(LD) $(LDFLAGS) start.o matmult.o -o matmult.coff
	../bin/coff2noff matmult.coff matmult
proj.o: proj.c
	$(CC) $(CFLAGS) -c proj.c
proj: proj.o start.o
	$(LD) $(LDFLAGS) start.o proj.o -o proj.coff
	../bin/coff2noff proj.coff proj
proj2.o: proj2.c
	$(CC) $(CFLAGS) -c proj2.c
proj2: proj2.o start.o
	$(LD) $(LDFLAGS) start.o proj2.o -o proj2.coff
	../bin/coff2noff proj2.coff proj2

ls.o: ls.c
	$(CC) $(CFLAGS) -c ls.c
ls: ls.o start.o
	$(LD) $(LDFLAGS) start.o ls.o -o ls.coff
	../bin/coff2noff ls.coff ls
pwd.o: pwd.c
	$(CC) $(CFLAGS) -c pwd.c
pwd: pwd.o start.o
	$(LD) $(LDFLAGS) start.o pwd.o -o pwd.coff
	../bin/coff2noff pwd.coff pwd
create.o: create.c
	$(CC) $(CFLAGS) -c create.c
create: create.o start.o
	$(LD) $(LDFLAGS) start.o create.o -o create.coff
	../bin/coff2noff create.coff create
open.o: open.c
	$(CC) $(CFLAGS) -c open.c
open: open.o start.o
	$(LD) $(LDFLAGS) start.o open.o -o open.coff
	../bin/coff2noff open.coff open
close.o: close.c
	$(CC) $(CFLAGS) -c close.c
close: close.o start.o
	$(LD) $(LDFLAGS) start.o close.o -o close.coff
	../bin/coff2noff close.coff close
read.o: read.c
	$(CC) $(CFLAGS) -c read.c
read: read.o start.o
	$(LD) $(LDFLAGS) start.o read.o -o read.coff
	../bin/coff2noff read.coff read
write.o: write.c
	$(CC) $(CFLAGS) -c write.c
write: write.o start.o
	$(LD) $(LDFLAGS) start.o write.o -o write.coff
	../bin/coff2noff write.coff write
